<?php
declare (strict_types = 1);

namespace app\index\controller;
use app\common\model\AdvertModel;
use app\common\model\SettingModel;
use app\common\model\MessagesModel;
use app\common\model\FriendlinkModel;
use app\common\model\NewsCategoryModel;
use app\common\model\NewsModel;
use app\common\model\TeamModel;
use app\common\model\UrlModel;
use think\facade\Request;
use think\facade\Route;
use think\facade\Lang;
use think\facade\View;
use think\facade\Db;

class Index extends Common
{
	/**
	 * @return \think\response\View
	 * @throws \think\db\exception\DataNotFoundException
	 * @throws \think\db\exception\DbException
	 * @throws \think\db\exception\ModelNotFoundException
	 */
    public function index()
    {

	    $advertList = AdvertModel::allList([['place','=',1]],999);
	    View::assign('advertList',$advertList);

	    $friendlinkList = FriendlinkModel::allList([],14);
	    View::assign('friendlinkList',$friendlinkList);
	    
	    $head_id = 'Index';
	    View::assign('head_id',$head_id);

	    $newsToppedList = NewsModel::getToppedList(6);
	    $newsRecommendedList = NewsModel::getRecommendedList(8);

	    $nMap = [];
	    $nMap[] = ['recommended','=',0];
	    $nMap[] = ['hotted','=',0];
	    $nMap[] = ['topped','=',0];
	    $nMap[] = ['status','=',1];
	    $newsList = NewsModel::pageList($nMap,6);

	    $i = 1;
	    $up = 0;
	    $newsRecommendedTree = [];
	    foreach ($newsRecommendedList as $key => $val) {
		    $newsRecommendedTree[$i]['sub'][] = $val;
		    if($up>=1){
				$up=0;
			    $i++;
		    }else{
			    $up++;
		    }
	    }

	    $i = 0;
	    $up = 0;
	    $newsTree = [];
	    foreach ($newsList as $key => $val) {
		    $newsTree[$i]['sub'][] = $val;
		    if($up==1){
			    $up=0;
			    $i++;
		    }else{
			    $up++;
		    }
	    }

        return view('',[
        	'newsToppedList' => $newsToppedList,
        	'newsRecommendedList' => $newsRecommendedList,
        	'newsRecommendedTree' => $newsRecommendedTree,
        	'newsList' => $newsList,
        	'newsTree' => $newsTree
        ]);
    }
    
    /**
    * AJAX异步流加载
    * 2021-11-21 00:13:00 沛霖主页
    */
    public function datalist(Request $request){
        $page_size = 12;  //每页显示条数
        $count = NewsModel::where('status',1)->count();  //总记录数
        $data['total_page'] = ceil($count/$page_size);  //总页数
        $cur_page = intval($request::param('page'))-1;  //默认前端page传过来为1 
        $nowTime    = time();
        $data['res'] = NewsModel::where('status',1)
            ->limit(($cur_page*$page_size),$page_size)  //limit默认要从零开始
            ->order('create_time','desc')
            ->select();
        foreach($data['res'] as $key=>$val){
            $val->newsMess = MessagesModel::where('status',1)->where('pid',$val->id)->where('user_id',0)->count();
	    	$currentCategory = NewsCategoryModel::find($val->category_id);
            $val->category_name = $currentCategory->title;  
	    	$val->ename = $currentCategory->ename;
	    	if($currentCategory->name == ''){
		        $val->listurl = UrlModel::getNewsUrl($currentCategory->category_id);
	    	}else{
		        $val->listurl = UrlModel::getNewsUrl($currentCategory->name);
	    	}
            if($val->id_domain == ''){
                $val->url = UrlModel::getShowUrl($val->id);
            }else{
                $val->url = UrlModel::getShowUrl($val->id_domain);
            }
            $val->create_time_y = date('Y',strtotime($val->create_time));
            $val->create_time_m = date('M',strtotime($val->create_time));
            $val->create_time_d = date('d',strtotime($val->create_time));
            $val->create_time_YMD_A = date('m-d-Y H:i A',strtotime($val->create_time));
            $val->create_time_text = strtotime($val->create_time)+259200 >$nowTime ? 'New' : '';
            $val->recommended_text = $val->recommended==1 ? '推荐' : '';
            $val->topped_text = $val->topped==1 ? '置顶' : '';
            $config = SettingModel::where('group','zh')->column('value','name');
            $val->system_message = $config['system_message'];
            $val->hide_post_views = $config['hide_post_views'];
            $val->post_style = $config['post_style'];
	    }
        return json($data);  //返回json
    }
    
    public function test(){
    	echo app()->getRuntimePath();
    	echo env('filesystem.storage', 'public/storage');
    }
    public function error()
    {
        
        return view();
    }
}
